package com.miage.itrading.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.miage.itrading.dao.CotationDAO;
import com.miage.itrading.entites.Cotation;
import com.miage.itrading.metier.Critere;

public class Screening extends JFrame {

	public static float X 		= 0.15f;
	public static float Y		= 0.1f;
	public static float WIDTH 	= 0.7f;
	public static float HEIGHT 	= 0.9f;

	public Component	courant;
	public JRadioButton	bVariation;
	public JRadioButton bGap;
	public JRadioButton bEnglobante;
	public JRadioButton bDojis;
	public JRadioButton bMarteaux;
	public JRadioButton bPendus;
	
	private static final long serialVersionUID = -6446709341276970792L;
	private ArrayList<Cotation> cotations;

	private JComboBox			sensVariation;
	private JComboBox			periodeVariation;
	private JTextField			valeurVariation;
	private JComboBox			choixTypeDeGap;
	private JComboBox			choixTypeEnglobante;
	private JComboBox			choixTypeChandelier;
	private JButton 			lancer;
	private FenetrePrincipale	parent;

	public Screening(FenetrePrincipale p)
	{
		courant = this;
		setTitle("Screening technique - iTrading");

		this.parent = p;

		setBackground(Color.white);
		setLocation((int) (parent.getScreenSize().width * X), 
				(int) (parent.getScreenSize().height * Y));

		setSize((int) (parent.getScreenSize().width * WIDTH), 
				(int) (parent.getScreenSize().height * HEIGHT));

		setResizable(true);
JPanel centrale = new JPanel(new GridLayout(5,1));
		/* Construction du Panel permettant la recherche des actions par variation */
		JPanel pVariation = new JPanel();
		bVariation = new JRadioButton();

		pVariation.add(bVariation);
		pVariation.add(new JLabel("Type de variation : "));
		sensVariation = sens();
		pVariation.add(sensVariation);

		pVariation.add(new JLabel("à "));
		valeurVariation = new JTextField();
		valeurVariation.setPreferredSize(new Dimension(45,25));
		pVariation.add(valeurVariation);
		pVariation.add(new JLabel("% "));

		pVariation.add(new JLabel("Période "));
		periodeVariation = periode();
		pVariation.add(periodeVariation);

		centrale.add(pVariation);

		/* Construction du panel pour le Gap Haussier/A la baisse */
		JPanel pGap = new JPanel();
		bGap = new JRadioButton();

		pGap.add(bGap);
		pGap.add(new JLabel("Gap : "));
		choixTypeDeGap = choixTypeGap();
		pGap.add(choixTypeDeGap);
		
		centrale.add(pGap);
		/* Construction du panel pour l'englobante Haussière/Baissiere */
		JPanel pEnglobante = new JPanel();
		bEnglobante = new JRadioButton();

		pEnglobante.add(bEnglobante);
		pEnglobante.add(new JLabel("Englobante : "));
		choixTypeEnglobante = choixTypeEnglobante();
		pEnglobante.add(choixTypeEnglobante);
		
		centrale.add(pEnglobante);
		
		/* Construction du panel pour les dojis */
		JPanel pDojis = new JPanel();
		bDojis = new JRadioButton();

		pDojis.add(bDojis);
		pDojis.add(new JLabel("Dojis "));
		
		centrale.add(pDojis);
		
		/* Construction du panel pour les marteaux/pendus */
		JPanel pChandelier = new JPanel();
		bMarteaux = new JRadioButton();

		pChandelier.add(bMarteaux);
		pChandelier.add(new JLabel("Chandeliers : "));
		choixTypeChandelier = choixTypeChandelier();
		pChandelier.add(choixTypeChandelier);
		centrale.add(pChandelier);
		
		add(centrale,BorderLayout.NORTH);
		/* Bouton pour la recherche */
		JPanel pBouton = new JPanel();
		pBouton.add(new JLabel());
		lancer = lancerRecherche();
		pBouton.add(lancer);
		pBouton.add(new JLabel());

		
		add(pBouton,BorderLayout.SOUTH);
		
		setVisible(true);
	}



	private void initData()
	{
		CotationDAO dao = new CotationDAO();
		cotations = new ArrayList<Cotation>();
		try {
			cotations = (ArrayList<Cotation>) dao.getAllCotations();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private JComboBox choixTypeGap(){
		JComboBox retour = new JComboBox();
		retour.addItem("Haussier");
		retour.addItem("Baissier");
		return retour;
	}
	
	private JComboBox choixTypeEnglobante(){
		JComboBox retour = new JComboBox();
		retour.addItem("Haussière");
		retour.addItem("Baissière");
		return retour;
	}
	
	private JComboBox choixTypeChandelier(){
		JComboBox retour = new JComboBox();
		retour.addItem("Marteaux");
		retour.addItem("Pendus");
		return retour;
	}

	private JComboBox sens(){
		JComboBox retour = new JComboBox();
		retour.addItem("Supérieur");
		retour.addItem("Inférieur");
		return retour;
	}

	private JComboBox periode(){
		JComboBox retour = new JComboBox();
		retour.addItem("1 jour");
		retour.addItem("1 semaine");
		retour.addItem("1 mois");
		retour.addItem("1 an");
		return retour;
	}

	private JButton lancerRecherche()
	{
		JButton all = new JButton("Lancer la recherche");

		all.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				initData();
				boolean lanceRecherche = false;
				int periode = periodeVariation.getSelectedIndex();
				if(bVariation.isSelected()){
					int sens = sensVariation.getSelectedIndex();
					try {
						Critere critVariation = new Critere(cotations);
						Float valeur = Float.parseFloat(valeurVariation.getText());


						if(critVariation.getActionsByVariation(sens, valeur, periode).isEmpty())
							JOptionPane.showMessageDialog(courant, "Pas de cotations répondant au critère recherché");
						else {
							cotations = critVariation.getActionsByVariation(sens, valeur, periode);
							lanceRecherche = true;
						}
					}
					catch (java.lang.NumberFormatException ex){
						JOptionPane.showMessageDialog(courant,"Veuillez saisir un pourcentage de variation correcte");
					}

				}
				if(bGap.isSelected())
				{
					Critere critGap = new Critere(cotations);
					int typeGap = choixTypeDeGap.getSelectedIndex();
					cotations = critGap.getActionsByGap(typeGap);
					lanceRecherche = true;
				}
				if(bEnglobante.isSelected())
				{
					Critere critEnglobante = new Critere(cotations);
					int typeEnglobante = choixTypeEnglobante.getSelectedIndex();
					cotations = critEnglobante.getActionsByEnglobante(typeEnglobante);
					lanceRecherche = true;
				}
				if(bDojis.isSelected())
				{
					Critere critDojis = new Critere(cotations);
					cotations = critDojis.getActionsByDojis();
					lanceRecherche = true;
				}
				if(bMarteaux.isSelected())
				{
					Critere critMarteaux = new Critere(cotations);
					if(choixTypeChandelier.getSelectedIndex()==0)
					{
					cotations = critMarteaux.getActionsByMarteaux();
					}
					if(choixTypeChandelier.getSelectedIndex()==1)
					{
					cotations = critMarteaux.getActionsByPendus();
					}
					lanceRecherche = true;
				}
				
				if(lanceRecherche && !cotations.isEmpty())new ResultScreening(cotations,courant,periode);
				else JOptionPane.showMessageDialog(courant, "Pas de cotations répondant au(x) critère(s) de recherche");
				
			}
		});

		return all;
	}




}
